package com.unisyou;

import com.unisyou.entity.PersonEntity;

import java.util.Optional;

public class Demo1 {
    public static void main(String[] args) {
        PersonEntity personEntity = new PersonEntity("C","K",25);
        test1(personEntity);

        PersonEntity personEntity1 = new PersonEntity();
        test2(personEntity1);
        test3(personEntity1);
    }

    /**
     * 1. 存在则开干
     */
    public static void test1(PersonEntity personEntity){
        Optional<PersonEntity> optionalPersonEntity = Optional.of(personEntity);
        optionalPersonEntity.ifPresent((p)-> System.out.println("实例1:"+p));
        /* java7
        if(personEntity != null){
            System.out.println(personEntity);
        }*/
    }

    /**
     * 2. 存在则返回,无则返回屁
     * @param personEntity
     */
    public static void test2(PersonEntity personEntity){
        Optional<PersonEntity> optionalPersonEntity = Optional.of(personEntity);
        PersonEntity personEntity1 = new PersonEntity("Z", "R", 30);
        System.out.println("实例2:"+optionalPersonEntity.orElse(personEntity1));
        if(personEntity != null){
            System.out.println(personEntity);
        }else{
            System.out.println(personEntity1);
        }
    }


    /**
     * 3. 存在则返回,无则由函数产生
     * @param personEntity
     */
    public static void test3(PersonEntity personEntity){
        Optional<PersonEntity> optionalPersonEntity = Optional.of(personEntity);
        System.out.println("实例3:"+optionalPersonEntity.orElseGet(()->createPersonEntity()));
    }

    private static PersonEntity createPersonEntity(){
        return new PersonEntity("Z", "R", 30);
    }
}
